package com.yunchao.bank.configure.data.repository

import com.yunchao.bank.configure.data.dao.ConfigureDao
import com.yunchao.bank.configure.data.model.toDomainModel
import com.yunchao.bank.configure.domain.model.ConfigureModel
import com.yunchao.bank.configure.domain.model.EMPTY_CONFIGURE_MODEL
import com.yunchao.bank.configure.domain.model.toEntityModel
import com.yunchao.bank.configure.domain.repository.ConfigureRepository

internal class ConfigureRepositoryImpl(
    private val configureDao: ConfigureDao
) : ConfigureRepository {

    override suspend fun saveConfigure(configure: ConfigureModel): Result<Boolean> = runCatching {
        configureDao.clearAll()
        when {
            configure.validateNetworkConfig() -> {
                configureDao.insertOrUpdate(configure.toEntityModel())
                true
            }

            else -> false
        }
    }

    override suspend fun getConfigure(): Result<ConfigureModel> = runCatching {
        configureDao.getConfig()?.toDomainModel() ?: EMPTY_CONFIGURE_MODEL
    }
}